﻿using System;
using System.Web.Security;
using System.Web.UI;
using FranceTriple.Dal;
using TeamFranceTriple.Config;

namespace TeamFranceTriple
{
    public partial class ShoutBox : BaseUserControl
    {
        private static Int32 MaxMessage = TeamFranceTripleConfig.WebConfig.shoutbox.MaxNumberOfMessage;
        private static Int32 AutoRefreshFrequency = TeamFranceTripleConfig.WebConfig.shoutbox.AutoRefresh;
        private String CurrentShoutText;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (AutoRefreshFrequency == 0)
                ShoutboxTimer1.Enabled = false;
            else if (!Request.Browser.IsMobileDevice && ThisMaster.RequesterIsAuthenticated)
                ShoutboxTimer1.Interval = AutoRefreshFrequency * 1000;

            if (Session["AnonymousID"] == null)
            {
                ShoutTextBox.Visible = true;
                SendButton.Visible = true;
                SmiliesButton.Visible = true;
            }
            else
            {
                ShoutTextBox.Visible = false;
                SendButton.Visible = false;
                SmiliesButton.Visible = false;
            }

            ShoutBoxListLink.NavigateUrl = "~/" + ThisMaster.RequesterCulture + "/shoutboxlist";
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            CurrentShoutText = ShoutboxDal.GetMessages(MaxMessage, ThisMaster.RequesterTimeZone);
            Literal1.Text = CurrentShoutText;
            ShowSmiliesLiteral.Text = ShoutboxDal.GetSmilies("DoSmilie");
        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            if (!this.Page.IsValid) return;

            if (ShoutTextBox.Text != String.Empty)
            {
                ShoutboxDal.CreateEntity(new FranceTriple.Dal.ShoutBox() { MessageShoutBox = ShoutTextBox.Text, UserNameAuthorShoutBox = ThisMaster.RequesterProfile.UserName });
                CurrentShoutText = ShoutboxDal.GetMessages(MaxMessage, ThisMaster.RequesterTimeZone);
                Literal1.Text = CurrentShoutText;
                ShoutTextBox.Text = String.Empty;
            }
        }
    }
}